
<div class="row">
    <div class="col">
        <div class="bd-callout bd-callout-info">
            <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-5 g-5 py-y">
                <div class="col d-flex align-items-start">
                    <div>
                        <h5 class="mb-0 fs-5">Current Sessions</h5>
                        <span class="metric"><span id="currentSessions"></span> ( <span id="activeSessions"></span> ) / <span id="totalSessions"></span></span>
                    </div>
                </div>
                <div class="col d-flex align-items-start">
                    <div>
                        <h5 class="mb-0 fs-5">MQTT-SN Out</h5>
                        <span class="metric"><span id="currentEgress"></span> Mp/s</span>
                    </div>
                </div>
                <div class="col d-flex align-items-start">
                    <div>
                        <h5 class="mb-0 fs-5">MQTT-SN In</h5>
                        <span class="metric"><span id="currentIngress"></span> Mp/s</span>
                    </div>
                </div>
                <div class="col d-flex align-items-start">
                    <div>
                        <h5 class="mb-0 fs-5">Connector Out</h5>
                        <span class="metric"><span id="currentConnector"></span> / <span id="totalConnector"></span> Mp/s</span>
                    </div>
                </div>
                <div class="col d-flex align-items-start">
                    <div>
                        <h5 class="mb-0 fs-5">Connector In</h5>
                        <span class="metric"><span id="currentConnectorIn"></span> Mp/s</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col">
        <div class="bd-callout bd-callout-info">
            <div class="row">
                <div class="col">
                    <div class="card card-metric">
                        <div class="card-chart">
                            <canvas id="publishMetrics" height="80"></canvas>
                        </div>
                        <div class="card-body">
                            <h6 class="card-title">MQTT-SN Publish Operations</h6>
                            <p class="card-text">The number of mqtt-sn publish messages sent and received in the time period.</p>
                        </div>
                    </div>
                </div>
                <div class="col">
                    <div class="card card-metric">
                        <div class="card-chart">
                            <canvas id="backendMetrics" height="80"></canvas>
                        </div>
                        <div class="card-body">
                            <h6 class="card-title">Connector Operations</h6>
                            <p class="card-text">Statistics relating to the backend connector performance. Includes snapshot queue size.</p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col">
                    <div class="card card-metric">
                        <div class="card-chart">
                            <canvas id="sessionMetrics" height="80"></canvas>
                        </div>
                        <div class="card-body">
                            <h6 class="card-title">MQTT-SN Sessions</h6>
                            <p class="card-text">The number of sessions in any one of the possible 5 states plotted over time.</p>
                        </div>
                    </div>
                </div>
                <div class="col">
                    <div class="card card-metric">
                        <div class="card-chart">
                            <canvas id="systemMetrics" height="80"></canvas>
                        </div>
                        <div class="card-body">
                            <h6 class="card-title">System</h6>
                            <p class="card-text">System information about the virtual machine memory and thread usage and networking layers.</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>

    var maxEntries = 100;

    bindChart('publishMetrics', "/console/chart?chartId=");
    // bindChart('networkMetrics', "/console/chart?chartId=");
    bindChart('backendMetrics', "/console/chart?chartId=");
    bindChart('sessionMetrics', "/console/chart?chartId=");
    bindChart('systemMetrics', "/console/chart?chartId=");

    function bindChart(el, d){

        $.getJSON(d + el, function(chartConfig) {
            var ctx = document.getElementById(el).getContext("2d");
            var chart = new Chart(ctx, chartConfig);
            var epoch = Date.now();
            pageTimer(updateTime, true, function(){
                $.getJSON(d + el + "&requestType=update&epoch=" + epoch, function (update){
                    // console.log(JSON.stringify(update));
                    if(update.labels.length > 0){
                        update.labels.forEach((label) => {
                            chart.data.labels.push(label)
                        });
                        chart.data.datasets.forEach((dataset, i) => {
                            Array.prototype.push.apply(dataset.data, update.data[i]);
                        });
                    } else {
                        // console.log("nothing to update with");
                    }
                    if(chart.data.labels.length > maxEntries){
                        removeData(chart, update.labels.length);
                    }
                    chart.update();
                });
                epoch = Date.now();
            })
        });
    }

    function removeData(chart, count) {
        for (let i = 0; i < count; i++) {
            chart.data.labels.shift();
            chart.data.datasets.forEach((dataset) => {
                dataset.data.shift();
            });
        }
        chart.update();
    }

    function updateField(id){
        updateHtmlElementUrlTimer(document.getElementById(id),
            fieldRefreshTime,
            "/console/metrics/field?field=" + id, true);
    }

    //dynafields
    updateField("currentSessions", fieldRefreshTime);
    updateField("activeSessions", fieldRefreshTime);
    updateField("totalSessions", fieldRefreshTime);
    updateField("currentEgress", fieldRefreshTime);
    updateField("currentIngress", fieldRefreshTime);
    updateField("currentConnector", fieldRefreshTime);
    updateField("totalConnector", fieldRefreshTime);
    updateField("currentConnectorIn", fieldRefreshTime);

</script>